<?php
namespace Admin\Controller;
use Common\Controller\AdminController;

class NewsController extends AdminController{
	private $user_new_cls_ids = "";
	protected function _initialize(){
		parent::_initialize();
		//根据用户的Gid（用户组ID）获取其信息权限
		$_gid = session('adminInfo.gid');
		$info = $this->getInfo('id,name,new_cls', 'group', "id='{$_gid}'");
		$this->user_new_cls_ids = $info['new_cls'];
	}

	//查找新闻分类数据集
	private function _set_classIds(){
		$_class_where = "";
		if($this->user_new_cls_ids){
			//设置用户的栏目权限范围
			$_class_where = "id in ({$this->user_new_cls_ids})";
		}
		return $_class_where;
	}

	public function index(){
		$where = '1=1';
		//根据当前用户所在的用户组信息，获取其用户组的栏目集合，实现栏目权限
		if($this->user_new_cls_ids){
			$where = "n.pid in ({$this->user_new_cls_ids})";
		}
		$where .= I('get.pid', -1) > -1 ? ' and n.pid=' . I('get.pid', 0, 'intval') :'';
		$where .= I('get.title') ? ' and n.title like "%' . I('get.title') . '%"' : '';
		$where .= I('get.status_query', -1) > -1 ? ' and n.status=' . I('get.status_query') : '';
		$where .= I('get.start_time') ? ' and n.times>=' . strtotime(I('get.start_time')) : '';
		$where .= I('get.end_time') ? ' and n.times<' . (strtotime(I('get.end_time')) + 24*3600) : '';
		
		$list = $this->getList('n.id,n.title,n.subtitle,n.times,n.view,n.sort,n.status,c.name', 'news n', $where, 
						'n.sort desc,n.id desc', true,12,"LEFT JOIN app_classify c on n.pid = c.id");

		$this->assign('list', $list);
		//查找新闻分类数据集
		$where = $this->_set_classIds();
		$classifyList = $this->getList('id,pid,name', 'classify', $where, 'sort desc,id asc');
		$this->sortNode($classifyList, $result);
		$this->assign('classifyList', $result);
		$this->display();

	}

	public function add(){

		if(IS_POST){
			if(!$_POST['pid']){
				$this->returnResult(false,"请维护分类");
			}
			$_POST['times'] = strtotime($_POST['times']);
			$result = $this->updateData($_POST, 'news');
			$this->returnResult($result);
		}else{
			//查找新闻分类数据集
			$where = $this->_set_classIds();
			$list = $this->getList('id,pid,name', 'classify', $where, 'sort desc,id asc');
			$this->assign('list', $list);
			$this->display();
		}
		
	}

	public function edit(){
		if(IS_POST){
			if(!$_POST['pid']){
				$this->returnResult(false,"请维护分类");
			}
			$_POST['pic'] = $_POST['pic'] ? $_POST['pic'] : '';
			$_POST['times'] = strtotime($_POST['times']);
			$result = $this->updateData($_POST, 'news', 2);
			$this->returnResult($result);
		}else{
			$info = $this->getInfo('*', 'news', 'id=' . I('get.id', 0, 'intval'));
			$this->assign('info', $info);
			//查找新闻分类数据集
			$where = $this->_set_classIds();
			$list = $this->getList('id,pid,name', 'classify', $where , 'sort desc,id asc');
			$this->sortNode($list, $result);
			$this->assign('list', $result);
			$this->display('add');
		}
	}

	public function del(){
		$result = $this->deleteData(array('id' => I('get.id', 0, 'intval')), 'news');
		$this->returnResult($result);
	}
}